home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Metarchivos / EnumMetafile / EnumMetafile.cs next >
Encoding:
Text File  |  2002-05-27  |  3.6 KB  |  116 lines

  1. //-------------------------------------------
  2. // EnumMetafile.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using System.Windows.Forms;
  10.  
  11. class EnumMetafile: Form
  12. {
  13.      Metafile     mf;
  14.      Panel        panel;
  15.      TextBox      txtbox;
  16.      string       strCaption;
  17.      StringWriter strwrite;
  18.  
  19.      public static void Main()
  20.      {
  21.           Application.Run(new EnumMetafile());
  22.      }
  23.      public EnumMetafile()
  24.      {
  25.           Text = strCaption = "Enumerar metarchivo";
  26.  
  27.                // Crear el cuadro de texto para mostrar registros.
  28.  
  29.           txtbox = new TextBox();
  30.           txtbox.Parent = this;
  31.           txtbox.Dock = DockStyle.Fill;
  32.           txtbox.Multiline = true;
  33.           txtbox.WordWrap = false;
  34.           txtbox.ReadOnly = true;
  35.           txtbox.TabStop = false;
  36.           txtbox.ScrollBars = ScrollBars.Vertical;
  37.  
  38.                // Crear el divisor entre el panel y el cuadro de texto.
  39.  
  40.           Splitter splitter = new Splitter();
  41.           splitter.Parent = this;
  42.           splitter.Dock = DockStyle.Left; // Derecha;
  43.  
  44.                // Crear el panel para mostrar el metarchivo.
  45.  
  46.           panel = new Panel();
  47.           panel.Parent = this;
  48.           panel.Dock = DockStyle.Left;
  49.           panel.Paint += new PaintEventHandler(PanelOnPaint);
  50.  
  51.                // Crear el men·.
  52.  
  53.           Menu = new MainMenu();
  54.           Menu.MenuItems.Add("í&Abrir!", new EventHandler(MenuOpenOnClick));
  55.      }
  56.      void MenuOpenOnClick(object obj, EventArgs ea)
  57.      {
  58.           OpenFileDialog dlg = new OpenFileDialog();
  59.           dlg.InitialDirectory = "c:\\";
  60.           dlg.Filter = "Todos los metarchivos|*.wmf;*.emf|" +
  61.                        "Metarchivo de Windows (*.wmf)|*.wmf|" +
  62.                        "Metarchivo mejorado (*.emf)|*.emf";
  63.  
  64.           if (dlg.ShowDialog() == DialogResult.OK)
  65.           {
  66.                try
  67.                {
  68.                     mf = new Metafile(dlg.FileName);
  69.                }
  70.                catch (Exception exc)
  71.                {
  72.                     MessageBox.Show(exc.Message, strCaption);
  73.                     return;
  74.                }
  75.                Text = strCaption + " - " + Path.GetFileName(dlg.FileName);
  76.                panel.Invalidate();
  77.  
  78.                     // Enumerar el metarchivo para el cuadro de texto.
  79.  
  80.                strwrite = new StringWriter();
  81.                Graphics grfx = CreateGraphics();
  82.  
  83.                grfx.EnumerateMetafile(mf, new Point(0, 0), 
  84.                     new Graphics.EnumerateMetafileProc(EnumMetafileProc));
  85.  
  86.                grfx.Dispose();
  87.                txtbox.Text = strwrite.ToString();
  88.                txtbox.SelectionLength = 0;
  89.           }
  90.      }
  91.      bool EnumMetafileProc(EmfPlusRecordType eprt, int iFlags,
  92.                            int iDataSize, IntPtr ipData, 
  93.                            PlayRecordCallback prc)
  94.      {
  95.           strwrite.Write("{0} ({1}, {2})", eprt, iFlags, iDataSize);
  96.  
  97.           if (iDataSize > 0)
  98.           {
  99.                byte[] abyData = new Byte[iDataSize];
  100.                Marshal.Copy(ipData, abyData, 0, iDataSize);
  101.  
  102.                foreach (byte by in abyData)
  103.                     strwrite.Write(" {0:X2}", by);
  104.           }
  105.           strwrite.WriteLine();
  106.           return true;
  107.      }
  108.      void PanelOnPaint(object obj, PaintEventArgs pea)
  109.      {
  110.           Panel    panel = (Panel) obj;
  111.           Graphics grfx  = pea.Graphics;
  112.  
  113.           if (mf != null)
  114.                grfx.DrawImage(mf, 0, 0);
  115.      }
  116. }